package parspack;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IProjectDescription;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.jdt.core.IClasspathEntry;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IPackageFragment;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.ui.PreferenceConstants;

public class ListTypes {

	ArrayList<MyCompilationUnit> compUnits;
	public ListTypes(Path args) throws Exception {

		IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(
				"JPR");
		project.create(null);
		project.open(null);

		IPath srcPath = new Path("src");
		IFolder srcFolder = project.getFolder(srcPath);
		srcFolder.createLink(args, IResource.ALLOW_MISSING_LOCAL, null);

		IClasspathEntry srcEntry = JavaCore.newSourceEntry(project
				.getFullPath().append(srcPath));
		IClasspathEntry[] jreEntry = PreferenceConstants.getDefaultJRELibrary();
		IClasspathEntry[] clEntry = new IClasspathEntry[jreEntry.length + 1];
		int i = 0;
		for (IClasspathEntry x : jreEntry) {
			clEntry[i] = x;
			i++;
		}
		clEntry[jreEntry.length] = srcEntry;

		IJavaProject projJava = JavaCore.create(project);

		IProjectDescription descr = project.getDescription();
		String[] oldDescription = descr.getNatureIds();
		String[] newDescription = new String[oldDescription.length + 1];
		newDescription[oldDescription.length] = JavaCore.NATURE_ID;
		oldDescription = newDescription;
		descr.setNatureIds(oldDescription);
		project.setDescription(descr, null);

		IFolder outFolder = project.getFolder(PreferenceConstants
				.getPreferenceStore().getString(
						PreferenceConstants.SRCBIN_BINNAME));
		outFolder.create(IResource.FORCE, true, null);
		outFolder.setDerived(true);

		project.refreshLocal(IResource.DEPTH_INFINITE, null);

		projJava.open(null);
		compUnits = new ArrayList<MyCompilationUnit>();
		IPackageFragment[] packageFragment = projJava.getPackageFragments();
		for (IPackageFragment ipf : packageFragment) {
			List<ICompilationUnit> list = new ArrayList<ICompilationUnit>();
			list.addAll(Arrays.asList(ipf.getCompilationUnits()));
			for(ICompilationUnit unit : list)
				compUnits.add(new MyCompFighter(unit));
		}
		
	}
}
